/*
 * Copyright 2013 Rivaldo.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jety.commons.util;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import java.lang.reflect.Method;
import org.jety.commons.exception.JsonRpcRemoteException;
import static org.jety.commons.util.JsonConverter.createJsonBuilder;
import org.jety.commons.domain.JsonRpcMethod;

/**
 *
 * @author Rivaldo
 */
public class CommunicationUtil {

    public static JsonObject creatMethodRequest(int id, String handleName, Method method, Object[] args) {
        String methodName = handleName + "." + method.getName();
        return createJsonMethodRequest(id, methodName, args);
    }

    public static JsonObject createResponse(int id, Object resultObject) {
        Gson gson = createJsonBuilder();

        JsonObject resp = new JsonObject();
        resp.addProperty(JsonRpcConstants.PROPERTY_VERSION, JsonRpcConstants.JSON_VERSION);
        resp.addProperty(JsonRpcConstants.PROPERTY_ID, id);

        JsonElement result = gson.toJsonTree(resultObject);

        resp.add(JsonRpcConstants.PROPERTY_RESULT, result);

        return resp;
    }

    public static JsonRpcMethod getRequestMethod(JsonObject request) throws JsonRpcRemoteException {
        Integer requestID = request.get(JsonRpcConstants.PROPERTY_ID).getAsInt();
        JsonRpcMethod method = getMetohd(request);
        return method;
    }

    private static JsonRpcMethod getMetohd(JsonObject jsonObject) throws JsonRpcRemoteException {
        Integer requestID = jsonObject.get(JsonRpcConstants.PROPERTY_ID).getAsInt();
        String methodName = jsonObject.getAsJsonPrimitive(JsonRpcConstants.PROPERTY_METHOD).getAsString();
        JsonArray params = (JsonArray) jsonObject.get(JsonRpcConstants.PROPERTY_PARAMS);

        return new JsonRpcMethod(requestID, methodName, params);
    }

    private static JsonObject createJsonMethodRequest(int id, String methodName, Object[] args) {
        Gson gson = createJsonBuilder();

        JsonObject req = new JsonObject();
        req.addProperty(JsonRpcConstants.PROPERTY_VERSION, JsonRpcConstants.JSON_VERSION);
        req.addProperty(JsonRpcConstants.PROPERTY_ID, id);
        req.addProperty(JsonRpcConstants.PROPERTY_METHOD, methodName);

        JsonArray params = new JsonArray();
        if (args != null) {
            for (Object o : args) {
                params.add(gson.toJsonTree(o));
            }
        }
        req.add(JsonRpcConstants.PROPERTY_PARAMS, params);

        return req;
    }
}
